package com.itcast.controller;


import com.itcast.common.res.ResultData;
import com.itcast.pojo.Address;
import com.itcast.service.Addressservice;
import com.itcast.service.impl.AddressServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.xml.bind.ValidationException;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@Controller
@RequestMapping("/address")
public class AddressController {

    @Autowired(required = false)
    private Addressservice addressservice;

    @PostMapping("/update")
    @ResponseBody
    public ResultData updateAddres(@RequestBody Map<String,String> map){
        ResultData rc=new ResultData();
        try {
            rc.setData(addressservice.updateAddres(map));
        }catch (ValidationException e){
            rc.setError(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
        return rc;
    }

    @GetMapping("/getAddresByUserId")
    @ResponseBody
    public ResultData getAddresByUserId(Integer userId){
        ResultData rc=new ResultData();
        try {
            Address addres = addressservice.getAddresByUserId(userId);
            rc.setData(addres);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rc;
    }
}

